package top.v5it.japi.plus.support;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.Arrays;

/**
 * 初始化当前应用程序所需要的基础数据
 *
 * @author zhanpu
 * @date 2020-11-9
 * @see org.springframework.context.ApplicationListener
 */
@Slf4j
public class JapiInitializedListener implements ApplicationListener<ContextRefreshedEvent> {

    /**
     * 是否抛出异常，true是，false否
     */
    @Setter
    private boolean loadThrow;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext context = event.getApplicationContext();

        Arrays.stream(context.getBeanNamesForType(JapiLoadRedisSet.class))
                .map(v -> context.getBean(v, JapiLoadRedisSet.class))
                .forEach(v -> v.loadSet(loadThrow));
    }
}
